using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Contents;
using NCS.Module.CMS.Library.Distributions;
using NCS.Library.Components.Partner;
using NCS.Module.CMS.Library.Events;

namespace NCS.Module.CMS.Web.Financial
{
    public partial class ContentDetail : CMSWebControl
    {
        public int height = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (curDistInfo == null) return;
                DistributionController.UpdateDistributionView(curDistInfo.Distribution_ID, 1);
                ContentInfo content = ContentController.GetContent(curDistInfo.Distribution_ContentID);

                if (content == null) return;

                if (content.Content_SubTitle != String.Empty)
                {
                    lblSubTitile.Text = content.Content_SubTitle + " <br><br>";
                }
                else
                {
                    lblSubTitile.Visible = false;
                }
                
                if (content.Content_Alias!=string.Empty && content.Content_Alias!="")
                {
                    lbpartner.Text = content.Content_Alias;
                    lbdate.Text = " - " + ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }
                else
                {
                    lbdate.Text = ConvertUtility.FormatTimeVn(curDistInfo.Distribution_CreateDate);
                }
                
                if (content.Content_AvatarSource > 0)
                {
                    PartnerInfo pinfo = PartnerController.GetPartner(content.Content_AvatarSource);
                    if (pinfo != null)
                    {
                        if (pinfo.Avatar != string.Empty)
                            imgpartner.ImageUrl = MultimediaUtility.GetRealPath(pinfo.Avatar);
                    }
                }

               
                lblSpecialHeadline.Text = content.Content_Headline;
                litSpecialTeaser.Text = content.Content_Teaser;
                litSpecialContentBody.Text = content.Content_Body;
                lbdescavatar.Text = content.Content_AvatarDescription;

                if (content.Content_Body.IndexOf("##BoxChuGiai##") > 0)
                {
                    litSpecialContentBody.Text = content.Content_Body.Replace("##BoxChuGiai##", content.Content_NoteBox);
                }
                else
                {
                    if (content.Content_NoteBox != string.Empty)
                    {
                        lblBoxComment.Text = content.Content_NoteBox;
                    }
                    else
                    {
                        lblBoxComment.Visible = false;
                    }
                }
                
                if (content.Content_Avatar != string.Empty)
                {
                    imgSpecialAvatar.ImageUrl = content.Content_Avatar;
                }
                else
                {
                    imgSpecialAvatar.Visible = false;
                    divAvatar.Visible = false;
                }
                //event
                DataTable dtEvent = EventController.Event_GetNewsByEventIDExcludeDistid(EventController.Event_GetEventIDByDistID(curDistInfo.Distribution_ID), 3, curDistInfo.Distribution_ID);
                if (dtEvent.Rows.Count < 1)
                {
                    pnlEvent.Visible = false;
                }
                else
                {
                    rptEvent.DataSource = dtEvent;
                    rptEvent.DataBind();
                }
                DataTable dtNewContent = DistributionController.GetNewDistributedContent(curDistInfo.Distribution_ID, 5);
                if (dtNewContent.Rows.Count < 1)
                {
                    pnlNewContent.Visible = false;
                }
                else
                {
                    rptNewContent.DataSource = dtNewContent;
                    rptNewContent.DataBind();
                }
            
                if (dtEvent.Rows.Count < 1 && dtNewContent.Rows.Count < 1)
                {
                    pnlOther.Visible = false;
                }
                
                //chum anh
                DataTable dtPic = DistributionController.Distributions_GetPicInAlbumByContentId(curDistInfo.Distribution_ContentID);

                if (dtPic.Rows.Count > 0)
                {
                    string strAvatar = ConvertUtility.ToString(dtPic.Rows[0]["PicAvatar"]);
                    FileInfo fileInfo = new FileInfo(Server.MapPath(strAvatar));
                    System.Drawing.Image orImg = System.Drawing.Image.FromFile(fileInfo.FullName);
                    height = orImg.Height;
                    
                    rptPic.DataSource = dtPic;
                    rptPic.DataBind();
                }
                else
                {
                    pnlSlide.Visible = false;
                }
            }

        }
        protected void rptEvent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");

                hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

                string tooltip = MiscUtility.ToolTip(ConvertUtility.ToString(currow["Content_Headline"]), ConvertUtility.ToString(currow["Content_SubTitle"]), ConvertUtility.ToString(currow["Content_Avatar"]), ConvertUtility.ToString(currow["Content_Teaser"]), ConvertUtility.ToString(currow["Content_Alias"]), ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                hplTitle.Attributes.Add("onmouseover", "showtip('" + tooltip + "','322','#f1f1f1')");
                hplTitle.Attributes.Add("onmouseout", "hidetip()");

                hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], curZone.Zone_ID);
            }
        }
        protected void rptNewContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                HyperLink hplTitle = (HyperLink)e.Item.FindControl("hplTitle");

                hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);

                string tooltip = MiscUtility.ToolTip(ConvertUtility.ToString(currow["Content_Headline"]), ConvertUtility.ToString(currow["Content_SubTitle"]), ConvertUtility.ToString(currow["Content_Avatar"]), ConvertUtility.ToString(currow["Content_Teaser"]), ConvertUtility.ToString(currow["Content_Alias"]), ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                hplTitle.Attributes.Add("onmouseover", "showtip('" + tooltip + "','322','#f1f1f1')");
                hplTitle.Attributes.Add("onmouseout", "hidetip()");

                hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], curZone.Zone_ID);
            }
        }
     
        protected void rptPic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;

                Image imgSlide = (Image)e.Item.FindControl("imgSlide");
                if (ConvertUtility.ToString(currow["PicAvatar"]) != string.Empty)
                {
                    imgSlide.ImageUrl = currow["PicAvatar"].ToString();
                }
                else
                {
                    imgSlide.Visible = false;
                }

                Label lblDesc = (Label) e.Item.FindControl("lblDesc");
                if (ConvertUtility.ToString(currow["PicDetail"]) != string.Empty)
                {
                    lblDesc.Text = currow["PicDetail"].ToString();
                }
                else
                {
                    lblDesc.Visible = false;
                }
            }
        }
    }
}